//
//  DistancesChartVC.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/26.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit
import Charts

class DistancesChartVC: StepsChartVC {
    
    override var yValGranularitie: Double? { 1 }

    override func loadDayData(_ date: Date) {
        API.getStepsData(date, .day, imei: imei) { [weak self] result in
            guard result.suc, let data = result.data else {
                self?.onLoadDataFailed(result.errDesc)
                return
            }
            self?.onLoadSummaryData(data.info)
            
            let entries = data.list.compactMap { unit -> ChartDataEntry? in
                guard let hour = unit.hour else { return nil }
                return ChartDataEntry(x: hour, y: unit.distance / 1000, data: unit)
            }
            self?.didLoadDayData(entries)
        }
    }
    
    override func loadWeekData(_ date: Date) {
        API.getStepsData(date, .week, imei: imei) { [weak self] result in
            guard result.suc, let data = result.data else {
                self?.onLoadDataFailed(result.errDesc)
                return
            }
            self?.onLoadSummaryData(data.info)
            
            let entries = data.list.compactMap { unit -> BarChartDataEntry? in
                guard let weekday = unit.weekday else { return nil }
                return BarChartDataEntry(x: Double(weekday), y: unit.distance / 1000, data: unit)
            }
            self?.didLoadWeekData(entries)
        }
    }
    
    override func loadMonthData(_ date: Date) {
        API.getStepsData(date, .month, imei: imei) { [weak self] result in
            guard result.suc, let data = result.data else {
                self?.onLoadDataFailed(result.errDesc)
                return
            }
            self?.onLoadSummaryData(data.info)
            
            let entries = data.list.compactMap { unit -> ChartDataEntry? in
                guard let day = unit.day else { return nil }
                return ChartDataEntry(x: Double(day), y: unit.distance / 1000, data: unit)
            }
            self?.didLoadMonthData(entries)
        }
    }
    
    override func loadYearData(_ date: Date) {
        API.getStepsYearlyData(date, imei: imei) { [weak self] result in
            guard result.suc, let data = result.data else {
                self?.onLoadDataFailed(result.errDesc)
                return
            }
            self?.onLoadSummaryData(data.info)
            let entries = data.list.compactMap { unit -> BarChartDataEntry? in
                guard unit.month > 0 else { return nil }
                return BarChartDataEntry(x: Double(unit.month), y: unit.totalDistance / 1000, data: unit)
            }
            self?.didLoadYearData(entries)
        }
    }
    
    override func onLoadSummaryData(_ info: ChartSummaryInfoModel?) {
        guard let info = info else { return }
        totalLabel?.text = String(format: "%.2f", info.totalDistance / 1000)
        averageLabel?.text = String(format: "%.2f", info.averageDistance / 1000)
    }
}
